(function(app){
	app
		.controller("content_54_inputCtrl",["$scope","$state","myFactory",'$stateParams',"$q",'$timeout','$location','$http','$ionicHistory',function($scope,$state,myFactory,$stateParams,$q,$timeout,$location,$http,$ionicHistory){
            //接收参数
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			$scope.chdrnum01=$stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			var item="";
			var item54="";
			var itemtemp="";
			var token=sessionStorage.getItem("token");
			$scope.readFlag = false;//阅读协议标志，默认为false
			$scope.deliverOptionSelect=null;

/**********************************************初始化函数begin****************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						$scope.chdrnum01=response.data.data.policy.base.chdrnum01;
						$scope.item54=response.data.data.modelMap.item54;
						item54=response.data.data.modelMap.item54;
						itemtemp=response.data.data.modelMap.itemtemp
	
						
						//默认勾选《申请须知》  改为不勾选
						//$scope.readFlag = false;
						
						//当前信函接收方式，并根据当前信函接收方式决定新的信函接收方式的初始化
						if(item54.deliverOption==''|| item54.deliverOption=='null' || $.trim(item54.deliverOption)==''){
							$scope.deliverOptionContent='无';
							$("#deliverOptionSelect").val('1');
							$scope.deliverOptionSelect = "1";
						}else if(item54.deliverOption=='1'){
							$scope.deliverOptionContent='电子邮件';
							$("#deliverOptionSelect").val('2');
							$scope.deliverOptionSelect = "2";
						}else if(item54.deliverOption=='2'){
							$scope.deliverOptionContent='纸质信函';
							$("#deliverOptionSelect").val('1');
							$scope.deliverOptionSelect = "1";
							//S2019012807158官微新增微信函 add by csf 2019-04-13 start	
						}else if(item54.deliverOption=='3'){
							//S2020060413341微信函使用率提升方案营运系统需求 add by yangyan 20200622 start
							$scope.deliverOptionContent='微信函';
							//S2020060413341微信函使用率提升方案营运系统需求 add by yangyan 20200622 end
							$("#deliverOptionSelect").val('1');
							$scope.deliverOptionSelect = "1";
						}
						//S2019012807158官微新增微信函 add by csf 2019-04-13 end 
						var options =$("#deliverOptionSelect option:selected");
						$scope.deliverOptionType=options.text();
						$("#deliverOptionType").val($scope.deliverOptionType);
						$("#newLetterFlag").val($scope.deliverOptionType);
						
						//选择方式初始化，原本逻辑是如果有缓存读取缓存，否则默认第一个，现在进行改动，默认选择跟当前信函接收方式不同的，相关逻辑整合在上面当前信函接收方式中
//						if(itemtemp.deliverOption==''|| itemtemp.deliverOption=='null' || $.trim(item54.deliverOption)==''){
//							$("#deliverOptionSelect").val('1');
//						}else{
//							$("#deliverOptionSelect").val(itemtemp.deliverOption);
//						}
						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
/**********************************************初始化函数begin****************************************************/
			
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//返回上一页
			$scope.goBack = function(){
				myFactory.goBack();
			}

			//关闭遮罩
			$scope.overwindowClose = function(){
				$(".overwindow").hide();
				$(".overmodal").hide();
			}

			//选择新的红利领取方式
			$scope.chooseLetter = function(){
				/*$.modal({
					title: "<p style='color:red'>请选择新的红利领取方式</p>",
					text: "<div class='weui-cells'><label class='weui-cell'><div class='weui-cell__hd'><input type='radio' name='welfare'>储存生息/留存公司</div></label><label class='weui-cell'><div class='weui-cell__hd'><input type='radio' name='welfare'>抵消保费</div></label></div>",
					onOK: function(text) {
			        }
				});*/
				$(".myDialog").fadeToggle(50);

				$(".myDialog_content label.weui-cell").click(function(){
					$(this).parents('.myDialog').fadeOut(50);
					var title = $(this).text();
					$("#letter").text(title);
				});
				$(".myDialog_black").click(function(){
					$(this).parent('.myDialog').fadeOut(50);
					$.toast("信函接收方式没有改变不需要改变", "text");
				});
			}
			//客户须知
			$scope.tipOpen = function(){
				modal('open','tip_model','overwindowOn54Input');
			}
			$scope.closeTip = function(){
				modal('close','tip_model','overwindowOn54Input');
			}

			//保单详情
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn54Input','overwindowOn54Input','policyCloseXOn54Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化查询订单详情
				var policyData;
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}

			//保单详情弹窗关闭
			$scope.policyCancelOn54Input = function(){
				modal('close','policyModalOn54Input','overwindowOn54Input','policyCloseXOn54Input');
			}

			$scope.toContentInputTbr10 = function () {
				$ionicHistory.clearCache().then(function(){
					$state.go('content_10_input_tbr',{
						changeTypeId:itemId,chdrnum01:chdrnum01,chdrcoy:chdrcoy,item10:item10
					});
				})
			}

			//客户须知弹框
			$scope.tipOpen = function(){
				modal('open','tip_model','overwindowOn54Input');
			}
			$scope.tipClose = function(){
				modal('close','tip_model','overwindowOn54Input');
			}
			//页面同意阅读协议复选框部分
			$scope.changeReadFlag = function(){
				$scope.readFlag = !$scope.readFlag;
				console.log($scope.readFlag);
			}

			$scope.deliverOptionChange = function(){
				var options = "";
				options =$("#deliverOptionSelect option:selected");
				$scope.deliverOptionType=options.text();
				console.log("test1:"+options.val());
				console.log("test2:"+options.text());
				//var v_jf = $("#idType"+type).val();
				//jQuery("#newLetterFlag").val(jQuery("#DeliverOption")[0].options[jQuery("#DeliverOption")[0].selectedIndex].value);
				$("#newLetterFlag").val(options.val());
			}

			//下一步
			$scope.nextStep = function(){
				//判断是否初始化完成了，如果失败，不予进入下一步
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}
				
				var  oldLetterFlag=$("#oldLetterFlag").val();
				if(oldLetterFlag!=="" && oldLetterFlag!=null){
					var oldLetterFlag = $("#oldLetterFlag").val();
					var newLetterFlag = $("#newLetterFlag").val();
					if(oldLetterFlag==newLetterFlag){
						//$("#pop").popup("open");
						//$("#tishi").html("信函接收方式没有改变不需变更");
						myFactory.alert('','信函接收方式没有改变不需变更','info');
						return false;
					}
				}

				var readFlag = $scope.readFlag;
				if(readFlag==false){
					myFactory.alert("","请阅读《申请协议》","info");
					return;
				}

				var options = $("#deliverOptionSelect option:selected");
				var deliverOption=options.val();
				//var deliverNm=options.text();
				//var item={"itemId":itemId,"DeliverOption":DeliverOption	};//"deliverNm":deliverNm,
				var item={"deliverOption":deliverOption};
				$ionicHistory.clearCache().then(function(){
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy
					});
				})
			}

		}]);

		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});

})(app)